package com.atguigu.test01;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

class Cooks{

    ReentrantLock lock = new ReentrantLock();
    private Integer cooks=500;

    public void save(){
        lock.lock();
            if(cooks<=0) {
                System.out.println(Thread.currentThread().getName()+" 已卖完");
                lock.unlock();
                return;
            }
        try {
            System.out.println(Thread.currentThread().getName()+" 原来有"+" "+cooks);
            cooks--;
            System.out.println(Thread.currentThread().getName()+" 现在有"+" "+cooks);
        } finally {
            lock.unlock();
        }
    }

    public Integer getCooks() {
        return cooks;
    }
}

public class CookByThreadPoolExecutor {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                4,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(6),
                Executors.defaultThreadFactory(),
                //new ThreadPoolExecutor.AbortPolicy()
                //new ThreadPoolExecutor.DiscardPolicy()
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        executor.submit(r);
                    }
                }
        );

        Cooks cooks = new Cooks();

        /*Future<Integer> future1 = threadPoolExecutor.submit(() -> {
            while (true){
                if(cooks.getCooks()<=0){
                    return 0;
                }
                cooks.save();
            }

        });

        Future<Integer> future2 = threadPoolExecutor.submit(() -> {
            while (true){
                if(cooks.getCooks()<=0){
                    return 0;
                }
                cooks.save();
            }
        });

        Future<Integer> future3 = threadPoolExecutor.submit(() -> {
            while (true){
                if(cooks.getCooks()<=0){
                    return 0;
                }
                cooks.save();
            }
        });*/

        /*for (int i = 0; i < 20; i++) {
            threadPoolExecutor.submit(() -> {
                while (true){
                    if(cooks.getCooks()<=0){
                        return 0;
                    }
                    cooks.save();
                }
            });
        }*/

        while (cooks.getCooks()>0){
            threadPoolExecutor.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    if(cooks.getCooks()<=0){
                        return 0;
                    }
                    cooks.save();
                    return 0;
                }
            });
        }

        threadPoolExecutor.shutdown();
    }
}
